﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Threading;
using System.Globalization;

public partial class NewProduct : System.Web.UI.Page
{
    List<string> fields = new List<string>();
    ArrayList values = new ArrayList();
    List<string> types = new List<string>();
    /////////////////////////////////////
    List<string> fields2 = new List<string>();
    ArrayList values2 = new ArrayList();
    List<string> types2 = new List<string>();
    string auther = "";
    string title = "";
    string desc = "";
    string topic = "";
    string pups = "";
    string issn = "";
    string dwoy = "";
    string lc = "";

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                

            }
        }

    }
    public void insertfld(string tag, string ins, string flds)
    {

        //////////////////////////////
        fields2.Add("tag");
        types2.Add("string");
        values2.Add(tag);
        ///////////////////////////////////////////////
        //////////////////////////////
        fields2.Add("indicator");
        types2.Add("string");
        values2.Add(ins);
        ///////////////////////////////////////////////
        //////////////////////////////
        fields2.Add("field");
        types2.Add("string");
        values2.Add(flds);
        ///////////////////////////////////////////////
        //////////////////////////////
    }
    public void ins(Product prod)
    {
        //////////////////////////////
        for (int i = 0; i < fields2.Count; i++)
        {
            string inds = "";
            string myfield = "";
            string mytag = "";
            string mysub = "";
            if (fields2[i] == "tag")
            {
                mytag = (string)values2[i];
                if (fields2[i + 1] == "indicator")
                {
                    inds = (string)values2[i + 1];
                    // DB.INSERTag((string)values2[i], inds, f);
                    i++;
                    if (fields2[i + 1] == "subfield")
                    {
                        mysub = (string)values2[i + 1];
                        i++;
                        if (fields2[i + 1] == "field")
                        {
                            myfield = (string)values2[i + 1];
                            i++;
                        }

                    }

                    else
                    {
                        if (fields2[i + 1] == "field")
                        {
                            myfield = (string)values2[i + 1];
                            i++;
                        }

                    }

                }
                else
                {
                    if (fields2[i + 1] == "subfield")
                    {
                        mysub = (string)values2[i + 1];
                        i++;
                        if (fields2[i + 1] == "field")
                        {
                            myfield = (string)values2[i + 1];
                            i++;
                        }

                    }

                    else
                    {
                        if (fields2[i + 1] == "field")
                        {
                            myfield = (string)values2[i + 1];
                            i++;
                        }

                    }

                }
                prod.importfield(mytag, inds, mysub, myfield);
                
            }

        }




    }
    public void switchs(string tag, string val)
    {



        switch (tag)
        {

            case "000":
                //// / //////auther////////////////////////////

                if (!fields.Exists(element => element == "product_type_id"))
                {
                    string vals = "";
                    val = val.Substring(7, 1);
                    vals = val == "s" ? "2" : "1";

                    fields.Add("product_type_id");
                    types.Add("int");
                    values.Add(vals);
                    /////////////
                }
                break;
            /////////////////////////////////////////////////
            case "008":
                //// / //////freq////////////////////////////

                if (!fields.Exists(element => element == "freq_id"))
                {
                    string vol = "";
                    val = val.Trim();
                    val = val.Substring(18, 1);
                    vol = freqswitch(val);
                    if (vol != "")
                    {
                        fields.Add("freq_id");
                        types.Add("int");
                        values.Add(vol);
                        ///////////////
                    }
                }
                break;
            ////////////////////////////////////////////////
            case "310":
                //// / //////freq////////////////////////////

                if (!fields.Exists(element => element == "freq_id"))
                {
                    string vol = "";
                    val = val.Trim();
                    // val = val.Split(' ').First(element => element.Contains("ly"));
                    vol = freqswitch2(val);
                    if (vol != "")
                    {
                        fields.Add("freq_id");
                        types.Add("int");
                        values.Add(vol);
                        ///////////////
                    }
                }
                break;
            ////////////////////////////////
            case "100":
                //// / //////auther////////////////////////////
                if (auther == "")
                {
                    fields.Add("auther1");
                    types.Add("string");
                    values.Add(val);
                    auther = val;
                }
                /////////////////////////////////////////////////
                break;
            ////////isbn//////////
            case "020":
                //////////////////
                if (issn == "")
                {

                    fields.Add("issn");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    //////////////////
                    if (!fields.Exists(element => element == "product_type_id"))
                    {
                        fields.Add("product_type_id");
                        types.Add("int");
                        values.Add(1);
                        ///////////////
                    }
                    issn = val;
                }
                break;
            //////issn//////
            case "022":
                //////////////////
                if (issn == "")
                {

                    fields.Add("issn");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    //////////////////
                    if (!fields.Exists(element => element == "product_type_id"))
                    {
                        fields.Add("product_type_id");
                        types.Add("int");
                        values.Add(2);
                        ///////////////
                    }
                    issn = val;
                }
                break;
            //////congress//////
            case "050":
                //////////////////
                if (lc == "")
                {

                    fields.Add("lc");
                    types.Add("string");
                    values.Add(val);
                    ///////////////

                    lc = val;
                }
                break;
            //////dewey//////
            case "082":
                //////////////////
                if (dwoy == "")
                {

                    fields.Add("dwoy");
                    types.Add("string");
                    values.Add(val);
                    ///////////////

                    dwoy = val;
                }
                break;
            case "240":
                ////////title//////////
                if (title == "")
                {
                    fields.Add("product");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    title = val;
                }
                break;
            ///////title///////////
            case "245":
                if (title == "")
                {
                    //////////////////
                    fields.Add("product");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    title = val;
                }
                break;
            ///////title//////////
            case "222":
                if (title == "")
                {
                    //////////////////
                    fields.Add("product");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    title = val;
                }
                break;
            case "650":
                //////////////////
                if (topic == "")
                {
                    fields.Add("topic");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    topic = val;
                }
                break;
            //////desc////////
            case "500":
                //////////////////
                if (desc == "")
                {
                    fields.Add("productdesc");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    desc = val;
                }
                break;
            /////////desc///////
            case "504":
                //////////////////
                if (desc == "")
                {
                    fields.Add("productdesc");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    desc = val;
                }
                break;
            /////////desc///////
            case "505":
                //////////////////
                if (desc == "")
                {
                    fields.Add("productdesc");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    desc = val;
                }
                break;
            /////////desc///////
            case "530":
                //////////////////
                if (desc == "")
                {
                    fields.Add("productdesc");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    desc = val;
                }
                break;
            /////////desc///////
            case "590":
                //////////////////
                if (desc == "")
                {
                    fields.Add("productdesc");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    desc = val;
                }
                break;
            case "260":
                if (pups == "")
                {
                    //////////////////
                    fields.Add("publisher");
                    types.Add("string");
                    values.Add(val);
                    ///////////////
                    pups = val;
                }
                break;
            ////////////////////////
            default:


                break;
        }



    }
    public string freqswitch(string vals)
    {

        switch (vals)
        {
            case "m":
                vals = "4";
                break;
            case "w":
                vals = "2";
                break;
            case "d":
                vals = "5";
                break;
            case "y":
                vals = "6";
                break;
            default:
                vals = "";
                break;

        }
        return vals;
    }
    public string freqswitch2(string vals)
    {


        vals = vals.ToLower();
        if (vals.Contains("monthly"))
        {
            vals = "4";
        }
        else
        {
            if (vals.Contains("weekly"))
            {
                vals = "2";

            }
            else
            {
                if (vals.Contains("daily"))
                {
                    vals = "5";
                }
                else
                {
                    if (vals.Contains("yearly"))
                    {

                        vals = "6";
                    }
                    else
                    {
                        vals = "";
                    }

                }
            }
        }
        //switch (vals.ToLower())
        //{
        //    case "monthly":
        //        vals = "4";
        //        break;
        //    case "weekly":
        //        vals = "2";
        //        break;
        //    case "daily":
        //        vals = "5";
        //        break;
        //    case "yearly":
        //        vals = "6";
        //        break;
        //    default:
        //        vals = "";
        //        break;

        //}
        return vals;
    }


    protected void save_Click(object sender, EventArgs e)
    {
        insertfld("000", "", leader.Text);
        insertfld("008", "", fix.Text);
        //////////////////////////////
        switchs("050", lcs.Text);
        insertfld("050", "", lcs.Text);
        /////////////////////////////////////
        //////////////////////////////
        switchs(DropDownList1.SelectedItem.Text.Split(',')[1], isn.Text);
        insertfld(DropDownList1.SelectedItem.Text.Split(',')[1], "", isn.Text);
        /////////////////////////////////////
        switchs("082", dewey.Text);
        insertfld("082", "", dewey.Text);
        ///////////////////////////////////
        switchs("260", pup.Text);
        insertfld("260", "", pup.Text);
        /////////////////////////////
        switchs("100", auth.Text);
        insertfld("100", "", auth.Text);
        //////////////////////////////////////
        //  insertfld("035", "", f35.Text);
        //////////////////////////////
        switchs("245", address.Text);
        insertfld("245", "", address.Text);
        /////////////////////////////////////////
        insertfld("300", "", f300.Text);
        ////////////////////////////////////////
        /////////////////////////////////////////
        // insertfld("310", "", f310.Text);
        insertfld("310", "", DropDownList2.SelectedValue);
        //////////////////
        if (DropDownList2.SelectedValue != null)
        {
            fields.Add("freq_id");
            types.Add("int");
            values.Add(int.Parse(DropDownList2.SelectedValue));
        }
        ///////////////
        //////////////////
        if (DropDownList3.SelectedValue != null)
        {
            fields.Add("classification_id");
            types.Add("int");
            values.Add(int.Parse(DropDownList3.SelectedValue));
        }
        ///////////////
        //////////////////
        if (DropDownList5.SelectedValue != null)
        {
            fields.Add("puplisher_id");
            types.Add("int");
            values.Add(int.Parse(DropDownList5.SelectedValue));
        }
        ///////////////
        //////////////////////
        //fields.Add("product_type_id");
        //types.Add("int");
        //values.Add(DropDownList1.SelectedValue);
        ///////////////////
        //////////////////////////////////////////
        /////////////////////////////////////////
        insertfld("005", "", f005.Text);
        ////////////////////////////////////////
        switchs("650", topics.Text);
        insertfld("650", "", topics.Text);
        //////////////////////////////////////
        switchs("500", notes.Text);
        insertfld("500", "", notes.Text);
        if (Grid1.AddedItems.Length > 0)
        {
            for (int i = 0; i < Grid1.AddedItems.Length; i++)
            {
                if ((Grid1.AddedItems[i].Cells[3].Value != null) && (Grid1.AddedItems[i].Cells[1].Value != null))
                {
                    string tag = Grid1.AddedItems[i].Cells[3].Value.ToString();
                    string ins = (Grid1.AddedItems[i].Cells[2].Value == null ? "" : Grid1.AddedItems[i].Cells[2].Value.ToString());
                    string fd = Grid1.AddedItems[i].Cells[1].Value.ToString();
                    insertfld(tag, ins, fd);
                }
            }
        }
        Product p = new Product();
        p.importfromvalues(fields, values); 
        this.ins(p);
        p.setobjecttodatabase(address.Text,int.Parse(Session["company_id"].ToString()), Session["user"].ToString().Split(',')[0]);
        DB.setpendingobjectscash();
        
        this.Response.Redirect("~/companypart/newproduct.aspx");
        
    }
}
